#include "ticketframe.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>
#include <QScrollArea>
#include <QSpacerItem>
#include <QVBoxLayout>
#include <QWidget>
#include "userordertestwindow.h"
#include <models.h>

TicketFrame::TicketFrame(QWidget *parent)
    : QWidget(parent)
{}

QString TicketFrame::formatTimeDifference(qint64 secondsDifference)
{
    // 计算小时数
    int hours = secondsDifference / (60 * 60);
    // 计算分钟数
    int minutes = (secondsDifference % (60 * 60)) / 60;

    // 格式化为 "X小时Y分钟"
    return QString("%1小时%2分钟").arg(hours).arg(minutes);
}
// int id, const QString& flightNumber, const QString& departure, const QString& destination,
//     const QString& departureAirport, const QString& destinationAirport,  // 新增参数
//     const QDateTime& departureTime, const QDateTime& arrivalTime, int firstClassSeats,
//     int economyClassSeats, double firstClassPrice, double economyClassPrice,
//     const QString& airlineCompany, const QString& aircraftModel

// const QString& airline, const QString& flightInfo, const QString& departureTime,
//     const QString& departureAirport, const QString& arrivalTime, const QString& arrivalAirport,
//     const QString& duration, const QString& price
//"南方航空", "CZ3085 空客320(中)", "14:45", "白云国际机场T2", "19:40",
// "羽田机场T3", "3小时55分", "¥5419起"
// 创建一个机票框组件
QWidget *TicketFrame::createTicketWidget(const Flight &flight,bool eco_class)
{
    QWidget *ticketWidget = new QWidget;

    ticketWidget->setStyleSheet(
        "border: 1px solid #ccc; padding: 10px; border-radius: 8px; background: white;");
    ticketWidget->setFixedHeight(100); // 固定高度

    QHBoxLayout *layout = new QHBoxLayout(ticketWidget);

    // 左侧航空公司Logo及名称
    // QPixmap logo(":/resources/logo.png"); // 替换为实际路径
    // QLabel* logoLabel = new QLabel;
    // logoLabel->setPixmap(logo.scaled(50, 50, Qt::KeepAspectRatio));
    QLabel *airlineLabel = new QLabel(
        QString("%1\n%2").arg(flight.airlineCompany, flight.aircraftModel));
    airlineLabel->setStyleSheet("font-weight: bold;");

    QVBoxLayout *airlineLayout = new QVBoxLayout;
    //airlineLayout->addWidget(logoLabel);
    airlineLayout->addWidget(airlineLabel);
    layout->addLayout(airlineLayout);

    // 中间航班信息
    QLabel *flightLabel = new QLabel(flight.flightNumber);
    QLabel *timeAndAirportLabel = new QLabel(
        QString("%1 -> %2   %3 -> %4")
            .arg(flight.departureTime.toString("HH:mm"), flight.arrivalTime.toString("HH:mm"))
            .arg(flight.departure, flight.destination));
    timeAndAirportLabel->setStyleSheet(
        "font-weight: bold; font-size:25px;");

    // 设置出发时间和到达时间
    QDateTime departureTime(flight.departureTime); // 2024-12-10 14:45
    QDateTime arrivalTime(flight.arrivalTime);     // 2024-12-10 19:40
    // 计算时间差（秒数）
    qint64 secondsDifference = departureTime.secsTo(arrivalTime);
    // 获取格式化后的时间差字符串
    QString formattedTime = formatTimeDifference(secondsDifference);
    QLabel *durationLabel = new QLabel(formattedTime);

    QHBoxLayout *infoLayout = new QHBoxLayout;
    infoLayout->addWidget(flightLabel);
    infoLayout->addWidget(timeAndAirportLabel);
    infoLayout->addWidget(durationLabel);
    infoLayout->setStretch(0, 1);
    infoLayout->setStretch(1, 2);
    infoLayout->setStretch(2, 1);
    layout->addLayout(infoLayout);

    // 右侧价格和订票按钮
    QLabel *priceLabel =(eco_class)? new QLabel(QString("¥%1 ").arg(flight.economyClassPrice, 0, 'f', 2)):
                             new QLabel(QString("¥%1 ").arg(flight.firstClassPrice, 0, 'f', 2)) ;
    priceLabel->setStyleSheet("color: orange; font-size: 16px; font-weight: bold;");
    QPushButton *bookButton = new QPushButton("订票");
    bookButton->setObjectName("bookButton");
    bookButton->setStyleSheet("background: orange; color: white; padding: 5px 15px;");
    QHBoxLayout *priceLayout = new QHBoxLayout;
    priceLayout->addWidget(priceLabel);
    priceLayout->addWidget(bookButton);
    priceLayout->setStretch(0, 3);
    priceLayout->setStretch(1, 1);
    layout->addLayout(priceLayout);

    // -------------------- 添加弹性空间以填充两侧 --------------------
    layout->addSpacerItem(new QSpacerItem(10, 40, QSizePolicy::Expanding, QSizePolicy::Minimum));

    // -------------------- 调整各部分拉伸比例 --------------------
    layout->setStretch(0, 1); // 左侧占 1 份
    layout->setStretch(1, 4); // 中间占 2 份
    layout->setStretch(2, 1); // 右侧占 1 份

    return ticketWidget;
}
